package com.clw.service.impl;


import com.clw.dataobject.Product;
import com.clw.repository.ProductRepository;
import com.clw.service.ProductService;
import com.clw.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-12-15.
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepository repository;

    @Override
    public boolean hasMore(String productName) {
        Product product=repository.findByName(productName);
        if (product!=null){
            return true;
        }
        return false;
    }

    @Override
    public List<Product> findAll() {
        return repository.findAll();
    }

    @Override
    public Product save(Product product) {
        return repository.save(product);
    }



    @Override
    public void del(Product product) {
        repository.delete(product);
    }

    @Override
    public Product add(Product product) {
        return repository.save(product);
    }

    @Override
    public Product findByProductName(String productName) {
        return repository.findByName(productName);
    }

    @Override
    public Product findOne(Integer id) {
        return repository.findOne(id);
    }

    @Override
    public Page<Product> findAll(String name, String catalog,String number, Pageable pageable) {
        return repository.findAll(getSpecification(name,catalog,number),pageable);
    }

    @Override
    public Long getSize(String name, String catalog,String number) {
        return repository.count(getSpecification(name,catalog,number));
    }

    private Specification<Product> getSpecification(String name, String catalog,String number){
        return new Specification<Product>() {
            @Override
            public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates=new ArrayList<>();
                if (StringUtils.isNotEmpty(name)){
                    predicates.add(criteriaBuilder.like(root.get("name"),"%"+name+"%"));
                }
                if (StringUtils.isNotEmpty(catalog)){
                    predicates.add(criteriaBuilder.like(root.get("catalog"),"%"+catalog+"%"));
                }
                if (StringUtils.isNotEmpty(number)){
                    predicates.add(criteriaBuilder.like(root.get("number"),"%"+number+"%"));
                }
                return criteriaQuery.where(predicates.toArray
                        (new Predicate[predicates.size()])).getRestriction();
            }
        };
    }
}
